import {
    Component, OnInit, EventEmitter, Output, Input, SimpleChanges, OnChanges, DoCheck, ViewChild,
} from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService,NzNotificationService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {UtilityService} from '../../service/utils.service';
import {appConfig} from '../../service/common';
@Component({
    selector: 'app-mergelist',
    templateUrl: './mergelist.component.html',
    styleUrls: ['./mergelist.component.less']
    })
export class MergelistComponent implements OnInit {

    // 变量
    errorId: any;
   
    // 输入属性
    @Input() // 输入属性,接受父组件传入的数据
    elementScice: any[];
    @Input()
    deliveryResult:any[];
    @Input()
    deliveryTime: any;
    @Input()
    isNext: boolean;
    @Input()
    iStouchan: boolean;
    @Input()
    istextVisible: boolean;
    @Input()
    checkStatus: boolean;
    @Input()
    title: any;
    @Input()
    isSpinning:boolean; // 加载框
    @Input()
    checkModalData:any[];// 工作项列表
    @Input()
    mergeListData: any[];

    @Input()
    isWorkitem: any;
    @Input()
    guid: any;
    @Input()
    checkloading: boolean;
    @Input()
    loading1: boolean;
    @Input()
    loading2: boolean;
    @Input()
    YpageIndex:number;// 异常当前页数
    @Input()
    YpageTotal: number;// 异常总页数
    @Input()
    token: any;
    @Input()
    initOptions:any[]// 级联框数组
    @Input()
     isNow :boolean;
    @Input()
     addPatchType:any[];
    @Input()
     isPro :string;
    @Output()
    // 输出属性
    isActive: EventEmitter<any> = new EventEmitter(); // 定义一个输出属性，当点击按钮的时候 发射出去
    @Output()
    buttonClick: EventEmitter<any> = new EventEmitter();
    @Output()
    mergeClick: EventEmitter<any> = new EventEmitter(); // 定义一个输出属性，当点击按钮的时候 发射出去
    @Output()
    returnsclick: EventEmitter<any> = new EventEmitter();
    @Output()
    checkSave: EventEmitter<any> = new EventEmitter();
    @Output()
    selected:EventEmitter<any> = new EventEmitter();
    @Output()
    loadDatamerge:EventEmitter<any> = new EventEmitter();
    @Output()
    pageNumber:EventEmitter<any> = new EventEmitter();
    @Output()
    chancanle:EventEmitter<any> = new EventEmitter();
     @Output()
    checkboxChild:EventEmitter<any> = new EventEmitter();
     @Output()
    checkboxPatch:EventEmitter<any> = new EventEmitter();
    arr = {
        guidDelivery: null,
        deployWhere: null,
        patchType: null

    }

 mergeListDataT = []
    deployType = []
guidDelivery:string;
   returnID: any;

  
    // deliveryTime
    constructor(private http: _HttpClient,
                public msg: NzMessageService,private _fb: FormBuilder,
                private utilityService: UtilityService,
                private nznot: NzNotificationService
                ) {
                   

    }

 
  nowData = []
  
  sonSelected(value){
    if(value.deliveryResult!=='核对中'){
        value.disabledS = true
    }else{
         value.disabledS = false
    }
    this.nowData = value;
  
   
    this.selected.emit(value);
      
  }




    ngOnInit() {
        this.elementScice = this.elementScice;
        this.checkloading = false;
    }
  
  soncheckbox(event,i){
      let obj={
              status:event,
              i:i
          }
          console.log( this.addPatchType )
  this.checkboxPatch.emit(obj);
  }
        
        soncheckboxChild(event,s,i){
          let obj={
              status:event,
              s:s,
              i:i
          }
            this.checkboxChild.emit(obj);
        }
  
    // 点击方法
    isClick(d, i) {

          if (d === true && i === true) {
            this.isActive.emit(i);
          }


    }

     onChange(item) {
        for (let i = 0; i < this.elementScice.length; i ++) {
            if (this.elementScice[i].guid !== item && this.elementScice[i].check === true) {
                this.elementScice[i].check = false;
            }
        }

    }

    sonmergeClick(index) {
        this.mergeClick.emit(index);
    }
    iStouchancanle(){
          this.chancanle.emit(this.iStouchan);
    }
    sonbuttonClick(index, it, suoyin) {
      
      this.guidDelivery = null


        switch(index){
            case 1:
               it.buttons = true;
             it.confirmStatus = '已合并'


             break;
              case 2:
               it.buttons = false;
             it.confirmStatus = '不投放'

             break;
              case 3:

            this.errorId = it.guid
             break;
              case 4:
            this.errorId = it.guid
             break;
        }

        let obj = {
            index:index,
            id : it,
            soyin: suoyin

        }
        this.buttonClick.emit(obj)
        console.log(this.addPatchType)
     
    }

    sonreturnsclick(index, it) {
        if (index === 0) {
            this.istextVisible = true
            this.returnID = it;
        }else {
            let obj = {
                index: index,
                it : it,
                        }
            this.returnsclick.emit(obj)
            if (index === 2 ) {
            this.istextVisible = false;
           }
        }


    }

    soncheckSave(){
          this.errorId;
           console.log(this.addPatchType)
        //    let otr = {exportType:'',}
         let arr = []
         let patchType = [];
           this.addPatchType.forEach(i=>{
               if(i.checked){
                  patchType.push(i.exportType)
                   let str = []
                   i.children.forEach(j=>{
                       if(j.checked){
                           str.push(j.name)
                       }
                   })
                  
            arr.push({exportType:i.exportType,deployType:str.join(',')})
               }
              
           })
         
            if(!this.guidDelivery || arr.length === 0){
                 this.nznot.create('error', '请填写完整信息！', '');
               
            }else{
                 let obj = {
                  arr: JSON.stringify(arr) ,
                  guidDelivery:this.guidDelivery,
                  errorId: this.errorId,
                  patchType:patchType.join(',')
              };
            
            
            this.checkSave.emit(obj); 
            }
        
           
    }
    loadData(event){
         this.loadDatamerge.emit(event);
    }

      pageChange(pi: number): Promise<any> {
         console.log(pi)
        this.YpageIndex = pi;
        this.checkloading = true;
        this.pageNumber.emit(pi); // 发射出去，把当前的页数发射出去
        return new Promise((resolve) => {
            setTimeout(() => {
                this.checkloading = false;
                resolve();
            }, 500);
        });
    }
   
 }
